﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xeml.Document.EnvironmentalOntology;
using System.Threading.Tasks;

namespace Xeml.Document.Ontologies.OBO
{
    public abstract class ContextLessHandler : OboHandler
    {
        #region constructors
        public ContextLessHandler()
            : base()
        {
        }
        #endregion constructors

        override protected void BuildContextes(ParsedOboFile collection)
        {
            Parallel.ForEach(collection.Where(x => x.isEnvironmentVariableTerm), currentXeoTermWithContext =>
            {
                XeoTerm currentXeoTerm = _terms.Single(x => x.TermId == currentXeoTermWithContext.Id) as XeoTerm;

                string ContextName = currentXeoTerm.Name;

                VariableContextSpec context = CreateQuantityContext(ContextName);
                currentXeoTerm.ContextCollection.Add(context.Name, context);
                currentXeoTermWithContext.contextCollection.Add(context);

                context = CreateResponseRangeContext(ContextName);
                currentXeoTerm.ContextCollection.Add(context.Name, context);
                currentXeoTermWithContext.contextCollection.Add(context);

                context = CreateFreeTextContext();
                currentXeoTerm.ContextCollection.Add(context.Name, context);
                currentXeoTermWithContext.contextCollection.Add(context);
            });
        }

        private VariableContextSpec CreateQuantityContext(string ContextName)
        {
            VariableContextSpec context = new VariableContextSpec();
            context.UnitSet = new UnitSet();
            context.TypeDefine = new TypeSpecification();
            context.Name = "Quantity";

            context.TypeDefine.BaseType = VariableDataTypes.Text;

            TypeSpecifics ts = new Xeml.Document.EnvironmentalOntology.Enum();
            ts.TextVal = ContextName;
            context.TypeDefine.TypeSpecifica.Add(ts);

            context.DefaultValue = ContextName;
            return context;
        }

        private VariableContextSpec CreateResponseRangeContext(string ContextName)
        {
            VariableContextSpec context = new VariableContextSpec();
            context.UnitSet = new UnitSet();
            context.TypeDefine = new TypeSpecification();
            context.Name = "ResponseRange";

            context.TypeDefine.BaseType = VariableDataTypes.Text;
            context.TypeDefine.TypeSpecifica.Add(new Min(0));
            context.TypeDefine.TypeSpecifica.Add(new Max(9));

            foreach (var item in Enumerable.Range(0, 10))
            {
                context.TypeDefine.TypeSpecifica.Add(new Xeml.Document.EnvironmentalOntology.Enum(item.ToString()));
            }
            context.DefaultValue = "5";
            return context;
        }

        private VariableContextSpec CreateFreeTextContext()
        {
            VariableContextSpec context = new VariableContextSpec();
            context.UnitSet = new UnitSet();
            context.TypeDefine = new TypeSpecification();
            context.Name = "FreeText";

            context.TypeDefine.BaseType = VariableDataTypes.Text;
            return context;
        }
    }
}
